package com.lv.fast.controller;

import com.lv.fast.constant.HttpConstant;
import com.lv.fast.constant.RestResultCodeConstant;
import com.lv.fast.constant.RestResultDescribeConstant;
import com.lv.fast.model.RestResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理filter错误的控制器
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/error")
public class ErrorController {

    /**
     * 未授权异常
     * @param request 请求对象
     * @return 错误对象
     */
    @RequestMapping("/unauthorized")
    public RestResult unauthorized(HttpServletRequest request){
        Object errorMessage = request.getAttribute(HttpConstant.REQUEST_ATTRIBUTE_ERROR_MESSAGE_KEY);
        Object errorCode = request.getAttribute(HttpConstant.REQUEST_ATTRIBUTE_ERROR_CODE_KEY);
        String message = RestResultDescribeConstant.AUTHORIZATION_TOKEN_INVALID;
        String code = RestResultCodeConstant.AUTHORIZATION_TOKEN_INVALID;
        if (errorMessage != null){
            message = errorMessage.toString();
        }
        if (errorCode != null){
            code = errorCode.toString();
        }
        return RestResult.build(code, message);
    }
}
